import com.alibaba.fastjson.JSON;
import com.cangshi.entity.Group;
import com.cangshi.entity.GroupUserKey;
import com.cangshi.service.ActivityService;
import com.cangshi.service.GroupService;
import com.cangshi.service.GroupUserService;
import com.cangshi.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Date;
import java.util.List;

/**
 * Created by Eoly on 2016/11/15.
 */

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {"classpath:spring-config.xml", "classpath:spring-mybatis.xml"})
public class UserTest {

    private static final Logger LOGGER = Logger.getLogger(UserTest.class);

//    @Autowired
//    private WorkService workService;
//
//    @Test
//    public void could_show() {
//        Work u = workService.findById(1);
//        LOGGER.debug("---------------------------------------------->>>>>>>>>>>"+ JSON.toJSON(u));
//    }

    @Autowired
    GroupService groupService;

    @Autowired
    GroupUserService groupUserService;

    @Autowired
    OrderService orderService;

    @Autowired
    ActivityService activityService;

    @Test
    public void jwt_add() {
        String name = "11515411651";
        String psw = "5649898984sad";
        byte[] a = new byte[]{15,52};
        SignatureAlgorithm signatureAlgorithm =SignatureAlgorithm.HS256;
        String jwtString = Jwts.builder()
                .setIssuer("cangshi")//设置发行人
                .setSubject("work")//设置抽象主题
                .setAudience("user")//设置角色
                .setExpiration(new Date(new Date().getTime()+999999999))//过期时间
                .setIssuedAt(new Date())//设置现在时间
                .setId("1")//版本1
                .signWith(signatureAlgorithm, new SecretKeySpec(a, 0, a.length, "AES"))
                .compact();
        System.out.println("--------------------------->"+jwtString);
    }

    @Test
    public void refresh(){
        String keyword = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjYW5nc2hpIiwic3ViIjoid29yayIsImF1ZCI6InVzZXIiLCJleHAiOjE0ODU2MzY0MTcsImlhdCI6MTQ4NDYzNjQxNywianRpIjoiMSJ9.Y1rFLOOKuSdrE0hpWAqoO170IfDK0S09CgwO1to_Aeg";
        byte[] a = new byte[]{15,42};
        Claims claims = Jwts. parser()
                .setSigningKey(new SecretKeySpec(a, 0, a.length, "AES"))
                .parseClaimsJws(keyword).getBody();
        System.out.println("--------------------------->"+claims);
    }

    @Test
    public void find(){
        Group group = new Group();
        PageHelper.startPage(4, 2);
        List<Group> groupList = groupService.findGroupsByGroup(group,null,null);
        PageInfo<Group> p=new PageInfo<Group>(groupList);
        System.out.println("--------------------------->"+JSON.toJSON(p));
    }

    @Test
    public void find_count(){
        Group group = new Group();
        group.setGroupId(1);
        LOGGER.debug("--------------------------->" + JSON.toJSON(groupService.findCountByGroup(null, "苹果")));
    }
    @Test
    public void find_num(){
        GroupUserKey groupUserCheck = new GroupUserKey();
        groupUserCheck.setGroupId(3);
        Integer groupNowNumber = groupUserService.getTheNumberGroupJoinedOrUserOwned(groupUserCheck);
        LOGGER.debug("--------------------------->" + groupNowNumber);
    }

    @Test
    public void find_deeply(){

        LOGGER.debug("--------------------------->" + JSON.toJSON(orderService.findByOrderIdDeeply(8)));
    }

    @Test
    public void find_by_joined(){

        Group checkGroup = new Group();
        checkGroup.setGroupStatus(1);

        LOGGER.debug("--------------------------->" + JSON.toJSON(groupUserService.findGroupsByUserIdCheckAndLimit(12, null, 1, 2)));

    }

    @Test
    public void test_order(){
        LOGGER.debug("--------------------------->" + JSON.toJSON(activityService.findActivitiesSimply(1)));
    }

}
